<?php
	include("includes/connect_db.php");
    $id_kontak = $_POST['id_kontak'];
    $val = $_POST[val];
    $aksi = $_GET['aksi'];

    switch ($aksi) {
    	case '1':
    			$sql_hapus_kontak = "DELETE FROM tb_kontak WHERE id_kontak = '$id_kontak'";
    			$query_hapus_kontak = mysql_query($sql_hapus_kontak);
				if($query_hapus_kontak){
                    $sql_rincian = "DELETE FROM tb_kontak_rincian WHERE id_kontak = '$id_kontak'";
                    $query_rincian = mysql_query($sql);
                    if ($query_rincian) {
                        echo "sukses";
                    }
				}
                $file = "img/resize_".$id_kontak.".jpg";
                $file_ico = "img/ico_".$id_kontak.".jpg";
                $file_thumb = "img/thumb_".$id_kontak.".jpg";

                unlink($file);
                unlink($file_ico);
                unlink($file_thumb);

                $sql_cek = "SELECT * FROM tb_detail_kontak WHERE id_kontak = '$id_kontak'";
                $query_cek = mysql_query($sql_cek);
                $jumlah = mysql_num_rows($query_cek);
                if ($jumlah > 0) {
                    $sql = "DELETE FROM tb_detail_kontak WHERE id_kontak = '$id_kontak'";
                    $query = mysql_query($sql);
                }

                $sql_privilege = "SELECT * FROM tb_privilege WHERE tipe = 'kontak' AND id_kontak = '$id_kontak'";
                $query_privilege = mysql_query($sql_privilege);
                $jumlah_privilege = mysql_num_rows($query_privilege);
                if ($jumlah_privilege > 0) {
                    $sql = "DELETE FROm tb_privilege WHERE tipe = 'kontak' AND id_kontak = '$id_kontak'";
                    $query = mysql_query($sql);
                }
    		break;

    	case '2':
    			$sql = "SELECT * FROM tb_kontak WHERE nama_kontak = '$val'";
                $query = mysql_query($sql);
                $jumlah = mysql_num_rows($query);
                if ($jumlah > 0) {
                    echo "val";
                }
    		break;

        case '3':
                if ($val != '') {
                    if (is_numeric($val) or $val[0] == '+') {
                        if ($val[0] == '0') {
                            $panjang = strlen($val);
                            if ($panjang > 12) {
                                echo "val1";
                            }
                        } elseif ($val[0] == '+') {
                            $panjang = strlen($val);
                            if ($panjang > 1 and $panjang <= 14) {
                                if ($val[1] == '6') {
                                    if (is_numeric($val)) {
                                        if ($panjang > 2) {
                                            if ($val[2] != '2') {
                                                echo "val1";
                                            }
                                        }
                                    } else {
                                        echo "val";
                                    }
                                } else {
                                    echo "val1";
                                }
                            } else {
                                echo "val1";
                            }
                        } else {
                            echo "val1";
                        }
                    } else{
                        echo "val";
                    }
                }
            break;

        case '4':
                $nama = $_POST['nama'];
                $email = $_POST['email'];
                $jenis_kelamin = $_POST['jenis_kelamin'];
                $alamat = $_POST['alamat'];
                $tanggal = $_POST['tanggal'];
                $nomor = $_POST['nomor'];

                if (empty($tanggal) and empty($jenis_kelamin)) {
                    $sql = "INSERT INTO tb_kontak (nama_kontak, email, alamat) 
                                        VALUES ('$nama', '$email', '$alamat')";
                } elseif (empty($tanggal)){
                    $sql = "INSERT INTO tb_kontak (nama_kontak, email, jenis_kelamin, alamat) 
                                        VALUES ('$nama', '$email', '$jenis_kelamin', '$alamat')";
                } elseif (empty($jenis_kelamin)) {
                    $sql = "INSERT INTO tb_kontak (nama_kontak, email, tanggal_lahir, alamat) 
                                        VALUES ('$nama', '$email', '$tanggal', '$alamat')";
                } else {
                    $sql = "INSERT INTO tb_kontak (nama_kontak, email, jenis_kelamin, tanggal_lahir, alamat) 
                                        VALUES ('$nama', '$email', '$jenis_kelamin', '$tanggal', '$alamat')";
                }                

                echo $sql;
                $query = mysql_query($sql);
                $id = mysql_insert_id();

                $i = 1;
                foreach ($nomor as $value) {
                    $no_hp = $value[0];

                    if ($no_hp[0] == '0') {
                        $panjang = strlen($no_hp);
                        $no_hp = "+62".substr($no_hp, 1, $panjang-1);
                    }

                    $nama_rincian = $nama." - Mobile ".$i;
                    $sql = "INSERT into tb_kontak_rincian (id_kontak, rincian, nama_rincian, no_hp, is_primary)
                            VALUES ('$id', '$i', '$nama_rincian', '$no_hp', '$value[1]')";
                    echo $sql;
                    $query = mysql_query($sql);
                    $i++;
                }
            break;

        case '5':
                $nama = $_POST['nama'];
                $email = $_POST['email'];
                $jenis_kelamin = $_POST['jenis_kelamin'];
                $alamat = $_POST['alamat'];
                $tanggal = $_POST['tanggal'];
                $nomor = $_POST['nomor'];

                if (empty($tanggal) and empty($jenis_kelamin)) {
                    $sql = "UPDATE tb_kontak SET nama_kontak = '$nama', email = '$email', alamat = '$alamat' WHERE id_kontak = '$id_kontak'";
                } elseif (empty($tanggal)){
                    $sql = "UPDATE tb_kontak SET nama_kontak = '$nama', email = '$email', jenis_kelamin = '$jenis_kelamin', alamat = '$alamat' WHERE id_kontak = '$id_kontak'";
                } elseif (empty($jenis_kelamin)) {
                    $sql = "UPDATE tb_kontak SET nama_kontak = '$nama', email = '$email', tanggal_lahir = '$tanggal', alamat = '$alamat' WHERE id_kontak = '$id_kontak'";
                } else {
                    $sql = "UPDATE tb_kontak SET nama_kontak = '$nama', email = '$email', jenis_kelamin = '$jenis_kelamin', tanggal_lahir = '$tanggal', alamat = '$alamat' WHERE id_kontak = '$id_kontak'";
                }                

                $query = mysql_query($sql);

                $sql_hapus = "DELETE FROM tb_kontak_rincian WHERE id_kontak = '$id_kontak'";
                $query_hapus = mysql_query($sql_hapus);

                $i = 1;
                foreach ($nomor as $value) {
                    $no_hp = $value[0];

                    if ($no_hp[0] == '0') {
                        $panjang = strlen($no_hp);
                        $no_hp = "+62".substr($no_hp, 1, $panjang-1);
                    }

                    $nama_rincian = $nama." - Mobile ".$i;
                    $sql = "INSERT into tb_kontak_rincian (id_kontak, rincian, nama_rincian, no_hp, is_primary)
                            VALUES ('$id_kontak', '$i', '$nama_rincian', '$no_hp', '$value[1]')";
                    echo $sql;
                    $query = mysql_query($sql);
                    $i++;
                }
            break;

    	case '6':
    			
    		break;
    }
?>
